﻿/**
* 书体容器类;
* @author crlnet
* @version 1.1
*/

package com.crlclr  {
	import caurina.transitions.Tweener;
	
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.data.*;
	import com.crlclr.events.*;
	import com.crlclr.managers.*;
	import com.crlclr.ui.*;
	import com.crlclr.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	
	public class Book extends Component{
		private var maskSize:QSize;
		private var data:AlbumData;
		
		private var pageList:Array;
		//private var defaultSizeBtn:QPButton;
		
		public var currentPage:Page;
		public var pageContainer:Sprite;
		
		public var bookMask:Shape;
		public var loader:Loader;
		
		
		public var bookState:String;
		public var isTooBig:Boolean;
		public var firstLoaded:Boolean;
		public function Book() {
			
			super();
		}
		
		override protected function defaults() : void{
			this.maskSize = new QSize(150, 150);
			pageList=[];
		}
		
		override protected function create():void {
			pageContainer = new Sprite();
			bookMask = new Shape();

			addChild(pageContainer);
			addChild(bookMask);
		}

		override protected function init():void{
			var g:Graphics = bookMask.graphics;
			g.clear();
			g.beginFill(0xff99cc);
			var size:QSize = maskSize.getHalf(-1);
			g.drawRect(size.width, size.height, maskSize.width, maskSize.height);
			this.mask = bookMask;
			
			
			ResizeableManager.getInstance().add(this);
		}
		
		
		private function bindContainerEvent():void {
			var stage:Stage = ResizeableManager.getInstance().stage;
			if(stage.hasEventListener(MouseEvent.MOUSE_MOVE)) {
				return;
			}
			stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHander);
		}
		private function removeContainerEvent():void {
			var stage:Stage = ResizeableManager.getInstance().stage;
			if(stage.hasEventListener(MouseEvent.MOUSE_MOVE)) {
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHander);
			}
		}
		private function moveHander(event:MouseEvent):void {
			var y:int = event.stageY;
			var x:int = event.stageX;
			var w:int = stage.stageWidth;
			var h:int = stage.stageHeight-45;
			
			var picW:int = this.currentPage.defaultSize.width;
			var picH:int = this.currentPage.defaultSize.height;
			
			var disX:int = x - w / 2;
			var disY:int = y - h / 2;
			
			
			if (Math.abs(disX) <=100 && picW>w) {
				var newX:int = ((w-picW)/ 200) * (disX + 100);
				this.pageContainer.x = newX;
			}
			if (Math.abs(disY) <=100 && picH>h) {	
				var newY:int = ((picH-h)/ 200) * (disY + 100);
				this.pageContainer.y = -newY;
			}
		}
		
		override public function move(endX:Number, endY:Number):void {
			Tweener.addTween(this.pageContainer, { x:endX, y:endY, time:1 } );
		}
		
		/**
		* 只写属性.
		* @param	data
		*/
		public function set dataProvier(data:AlbumData):void{
			this.data = data;
			this.firstLoaded = false;
			this.createPages();
		}
		public function close():void {
			Tweener.addTween(bookMask, {time:1, width:maskSize.width, height:maskSize.height,onComplete:oncloseCompleteHandler });
		}
		
		/**
		 * 当关闭完成时; 
		 * 
		 */		
		private function oncloseCompleteHandler():void {
			while (pageContainer.numChildren) {
				pageContainer.removeChildAt(pageContainer.numChildren - 1);
			}
			currentPage=null;
			
			dispatchEvent(new BookEvent(BookEvent.CLOSEBOOK));
		}
		
		private function createPages():void {
			var len:int =this.data.length;
			
			var buffer:Array=pageList.concat();
			pageList.splice(0);
			
			var page:Page
			for (var i:int = 0; i < len; i++) {
				if(buffer.length>0){
					page=buffer.pop();
				}else{
					page=new Page(this);
				}
				page.index=i;
				page.data=data.getPageData(i);
				pageList.push(page);
			}

			bookMask.scaleX = bookMask.scaleY = 1;
		}
		
		
		public function pageChange(event:BookEvent):void {
			switch(event.type) {
				case BookEvent.NEXT :
					loadNextPage();
					break;
				case BookEvent.PREV :
					loadPrevPage();
					break;
				case BookEvent.THUMBCHANGET :
					this.loadPage(event.index);
			}
		}
		
		public function loadPageAt(index:int):void {
			this.loadPage(index);
		}
		
		public function loadNextPage():Boolean {
			if(this.currentPage==null)return false;
			return loadPage(this.currentPage.index+1);
		}
		public function loadPrevPage():Boolean {
			if(this.currentPage==null)return false;
			return loadPage(this.currentPage.index-1)
		}
		
		public function loadPage(index:int):Boolean {
			if (index < 0 || index >= length)
			{
				return false;
			}
			
			//都让它自动适应;
			//bookState = BookState.AUTOSIZE;
			removeContainerEvent();
			
			var tempPage:Page = this.getPage(index);
			if (firstLoaded) {
				tempPage.alpha = 0;
			}
			if (tempPage.isLoaded) {
				showEfffect(tempPage);
				//直接发布完成事件;
				this.dispatchEvent(new BookEvent(BookEvent.COMPLETE,tempPage));
			}else{
				tempPage.addEventListener(Event.COMPLETE, initHander);
				tempPage.putin();
			}
			this.dispatchEvent(new BookEvent(BookEvent.PAGECHANGE, tempPage));
			return true;
		}
		
		private function initHander(event:Event):void {
			var tempPage:Page = event.target as Page;
			tempPage.removeEventListener(Event.INIT, initHander);
			showEfffect(tempPage);
			this.dispatchEvent(new BookEvent(BookEvent.COMPLETE, tempPage));
			
			if (firstLoaded) {
				return;
			}
			firstLoaded = true;
			dispatchEvent(new PageEvent(PageEvent.FIRSTLOADED));
			Tweener.addTween(bookMask, { time:3, scaleX:10, scaleY:10 } );
		}
		
		private function showEfffect(page:Page):void {
			if ( currentPage != null ) 
			{
				currentPage.removeEventListener ( Event.ENTER_FRAME, showImageOnEF );
				currentPage.addEventListener ( Event.ENTER_FRAME, hideImageOnEF );
			}
			page.addEventListener (Event.ENTER_FRAME, showImageOnEF);
			pageContainer.addChild ( page);
			currentPage = page;
		}
		
		private function hideImageOnEF ( e:Event ):void{
			var b:* = e.currentTarget;
			b.alpha += ( 0 - b.alpha ) * 0.2;
			if ( b.alpha < 0.15 )
			{
				b.alpha = 0;
				b.removeEventListener ( Event.ENTER_FRAME, hideImageOnEF );
				pageContainer.removeChild ( b );
			}
		}
		
		private function showImageOnEF ( e:Event ):void{
			var b:* = e.currentTarget;
			b.alpha += 0.1;
			
			if ( b.alpha > .97 )
			{
				b.alpha = 1;
				b.removeEventListener ( Event.ENTER_FRAME, showImageOnEF );
			}
		}
		
		override public  function resize(w:int,h:int):void{
			bookMask.x = w>>1;
			bookMask.y = h / 2 - this.y;
		}
		
		
		public function getData():AlbumData {
			return this.data;
		}		
		public function getlist():Array{
			return this.pageList;
		}
		public function getPage(index:int):Page{
			return this.pageList[index] as Page;
		}
		public function get length():int{
			return this.pageList.length
		}
		
		
		
	}
}